#    Copyright 2008, 2009 Tri-County Electric Cooperative, St. Matthews, South Carolina
#    This file is part of COOPGIS.
#    COOPGIS is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#    COOPGIS is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#    You should have received a copy of the GNU General Public License
#    along with COOPGIS.  If not, see <http://www.gnu.org/licenses/>.

import os
import datetime
FTPFileName='/opt/coopgis/fromgps/anonymousftproot_OtherReadOnly/incomingftpallpermissions/Locations.csv'
import shutil

#************************BEGIN Add Include Files to Path************************
import sys
NeededPaths=['/opt/coopgis/djangoproject/include', '/opt/coopgis/djangoproject']
for path in NeededPaths:
    if(not path in sys.path):
        sys.path.append(path)
#************************END   Add Include Files to Path************************
from django.contrib.gis.geos import *
import os, time, psycopg2, datetime
#********************Setup psycopg2 and django section********************
PathDjangoRoot="/opt/coopgis/"
if(not PathDjangoRoot in sys.path):
    sys.path.append(PathDjangoRoot)
import djangoproject
from djangoproject.models import *
#********************Setup psycopg2 and django section********************
import COOPGIS_Ajax, COOPGIS_GPS, COOPGIS_Misc, COOPGIS_ReportLab, COOPGIS_String, COOPGIS_Update, COOPGIS_FormMisc
from django.template import Context, loader
from django.http import HttpResponse
from django import forms
import os
import datetime
import djangoproject.settings



def BuildInventoryCollectionForm(DefaultMap, Shortcuts):
    Dict={}
    #Build Map Choices List
    MapChoices=[]
    MapChoices.append([DefaultMap.id, str(DefaultMap.Name)[:30]])
    Maps=Map.objects.all()
    for i in Maps:
        if(not i == DefaultMap):
            MapChoices.append([i.id, str(i.Name)[:30]])
    #Create Station Compatible Unit Choices
    StationCompatibleUnitChoices=[]
    StationCompatibleUnits=CompatibleUnit.objects.filter(StationIndicator=True).order_by('Name')
    StationCompatibleUnitChoices.append(['-1', 'None'])
    for i in StationCompatibleUnits:
        StationCompatibleUnitChoices.append([i.id, str(i)])
    #Create the MeterFormChoices
    MeterFormChoices=[]
    MeterForms=MeterForm.objects.all().order_by('Name')
    MeterFormChoices.append(['-1', 'None'])
    for i in MeterForms:
        MeterFormChoices.append([i.id, str(i)])
    #Create the MeterServiceTypeChoices
    MeterServiceTypeChoices=[]
    MeterServiceTypes=MeterServiceType.objects.all().order_by('Name')
    MeterServiceTypeChoices.append(['-1', 'None'])
    for i in MeterServiceTypes:
        MeterServiceTypeChoices.append([i.id, str(i)[:12]])
    #Create Span Compatible Unit Choices
    SpanCompatibleUnitChoices=[]
    SpanCompatibleUnits=CompatibleUnit.objects.filter(SpanIndicator=True).order_by('Name')
    SpanCompatibleUnitChoices.append(['-1', 'None'])
    for i in SpanCompatibleUnits:
        SpanCompatibleUnitChoices.append([i.id, str(i)])
    #Create StationAssemblyManufacturer Choices
    StationAssemblyManufacturerChoices=[]
    StationAssemblyManufacturerCompanies=Company.objects.all().filter(ManufacturerIndicator=True).order_by('Name')
    StationAssemblyManufacturerChoices.append(['-1', 'None'])
    for i in StationAssemblyManufacturerCompanies:
        StationAssemblyManufacturerChoices.append([i.id, str(i)[:4]])
    #Create StationAssemblyOwner Choices
    StationAssemblyOwnerChoices=[]
    StationAssemblyOwners=AssemblyOwner.objects.all().order_by('Name')
    StationAssemblyOwnerChoices.append(['-1', 'None'])
    for i in StationAssemblyOwners:
        StationAssemblyOwnerChoices.append([i.id, str(i)[:4]])
    #Create the PoleTreatmentTypeChoices
    PoleTreatmentTypeChoices=[]
    PoleTreatmentTypes=PoleTreatmentType.objects.all().order_by('Name')
    PoleTreatmentTypeChoices.append(['-1', 'None'])
    for i in PoleTreatmentTypes:
        PoleTreatmentTypeChoices.append([i.id, str(i)])
    #Create the PoleTreatmentCompanyChoices
    PoleTreatmentCompanyChoices=[]
    PoleTreatmentCompanies=Company.objects.all().filter(PoleTreatmentIndicator=True).order_by('Name')
    PoleTreatmentCompanyChoices.append(['-1', 'None'])
    for i in PoleTreatmentCompanies:
        PoleTreatmentCompanyChoices.append([i.id, str(i)])
    #Create Right Of Way Status Choices
    RightOfWayStatusChoices=[]
    RightOfWays=PoleRightOfWayStatus.objects.all().order_by('Name')
    RightOfWayStatusChoices.append(['-1', 'None'])
    for i in RightOfWays:
        RightOfWayStatusChoices.append([i.id, str(i)])
    for i in range(0, 10): #Number of Station Assemblies Listed On the Form
        Dict['StationAssemblyCompatibleUnit' + str(i)]=forms.ChoiceField(StationCompatibleUnitChoices, required=False)
        Dict['StationAssemblyCircuitNumber' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'2.5'}))
        Dict['StationAssemblyPhaseAIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyPhaseBIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyPhaseCIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyPhaseNeutralIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyPrimaryIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblySecondaryIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyServiceIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyOpenIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyRotation' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'2.5'}))
        Dict['StationAssemblyMeterNumber' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'2.5'}))
        Dict['StationAssemblySerialNumber' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'1.5'}))
        Dict['StationAssemblyMeterOnPoleIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyMeterDCSINumber' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'2.5'}))
        Dict['StationAssemblyMeterForm' + str(i)]=forms.ChoiceField(choices=MeterFormChoices, required=False)
        Dict['StationAssemblyMapNumber' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'2.5'}))
        Dict['StationAssemblyOldMapNumber' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'2.5'}))
        Dict['StationAssemblyOwner' + str(i)]=forms.ChoiceField(choices=StationAssemblyOwnerChoices, required=False)
        Dict['StationAssemblyManufacturer' + str(i)]=forms.ChoiceField(choices=StationAssemblyManufacturerChoices, required=False)    
        Dict['StationAssemblyTagNumber' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'4'}))
        Dict['StationAssemblyPutIntoServiceDateYear' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'2'}))    
        Dict['StationAssemblyPrimaryIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblySecondaryIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyPoleStubIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyPoleLightIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyPoleMeterIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyMeterServiceType' + str(i)]=forms.ChoiceField(choices=MeterServiceTypeChoices, required=False)
        Dict['StationAssemblyIdleIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['StationAssemblyPhysicalAddress' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'12'}))
        Dict['StationAssemblyNumberOfCopies' + str(i)]=forms.IntegerField(required=False, widget=forms.TextInput(attrs={'size':'1'}))
        Dict['StationAssemblyTransformerCutOutIndicator' + str(i)]=forms.BooleanField(required=False)
    for i in range(0, 3):
        Dict['SpanAssemblyCompatibleUnit' + str(i)]=forms.ChoiceField(SpanCompatibleUnitChoices, required=False)
        Dict['SpanAssemblyCircuitNumber' + str(i)]=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'2.5'}))
        Dict['SpanAssemblyPhaseAIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['SpanAssemblyPhaseBIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['SpanAssemblyPhaseCIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['SpanAssemblyPhaseNeutralIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['SpanAssemblyPrimaryIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['SpanAssemblySecondaryIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['SpanAssemblyServiceIndicator' + str(i)]=forms.BooleanField(required=False)
        Dict['SpanAssemblyIdleIndicator' + str(i)]=forms.BooleanField(required=False)
    #General Fields for All Stations/Spans
    Dict['Map']=forms.ChoiceField(MapChoices, required=False)
    Dict['SelectStationTagNumber']=forms.CharField(max_length = 25, required=False, widget=forms.TextInput(attrs={'size':'4'}))
    Dict['SelectStationid']=forms.CharField(max_length = 25, required=False, widget=forms.TextInput(attrs={'size':'3'}))
    Dict['SelectGPSIndicator']=forms.BooleanField()
    Dict['CreateStationIndicator']=forms.BooleanField()
    Dict['MoveStationGPSIndicator']=forms.BooleanField()
    Dict['MoveStationNorth']=forms.FloatField(required=False, widget=forms.TextInput(attrs={'size':'2'}))
    Dict['MoveStationEast']=forms.FloatField(required=False, widget=forms.TextInput(attrs={'size':'2'}))
    Dict['StationComment']=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'9'}))
    Dict['UpdateStationCommentIndicator']=forms.BooleanField(required=False)
    Dict['DeleteStationIndicator']=forms.BooleanField(required=True)
    Dict['SelectSpanid']=forms.IntegerField(required=False, widget=forms.TextInput(attrs={'size':'4'}))
    Dict['SpanFarStation']=forms.IntegerField(required=False, widget=forms.TextInput(attrs={'size':'5'}))
    Dict['CreateSpanIndicator']=forms.BooleanField(required=False)
    Dict['SpanComment']=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'9'}))
    Dict['SpanTapIndicator']=forms.BooleanField(required=False)
    Dict['UpdateSpanCommentIndicator']=forms.BooleanField(required=False)
    Dict['SpanJointTrenchIndicator']=forms.BooleanField(required=False)
    Dict['DeleteSpanIndicator']=forms.BooleanField(required=False)
    Dict['DeleteAssembly']=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'9'}))
    Dict['DeleteKeep']=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'9'}))
    Dict['PoleRightOfWayStatus']=forms.ChoiceField(choices=RightOfWayStatusChoices, required=False)
    Dict['PoleTreatmentType']=forms.ChoiceField(PoleTreatmentTypeChoices, required=False)
    Dict['PoleTreatmentCompany']=forms.ChoiceField(PoleTreatmentCompanyChoices, required=False)
    Dict['PoleTreatmentDateYear']=forms.CharField(required=False, widget=forms.TextInput(attrs={'size':'3'}))
    #Create a shortcut dictionary that has each shortcut along with the units that the shortcut will add
    for i in Shortcuts:
        Dict['StationAssemblyShortcut' + i + 'CreateIndicator']=forms.BooleanField(required=False)
        Dict['StationAssemblyShortcut' + i + 'NumberOfCopies']=forms.IntegerField(required=False, widget=forms.TextInput(attrs={'size':'1'}))
        Dict['StationAssemblyShortcut' + i + 'CircuitNumber' ]=forms.IntegerField(required=False, widget=forms.TextInput(attrs={'size':'1'}))
        Dict['StationAssemblyShortcut' + i + 'PrimaryIndicator']=forms.BooleanField(required=False)
        Dict['StationAssemblyShortcut' + i + 'SecondaryIndicator']=forms.BooleanField(required=False)
        Dict['StationAssemblyShortcut' + i + 'ServiceIndicator']=forms.BooleanField(required=False)    
    #Create PoleJointUseType Choices
    PoleJointUseTypeChoices=[]
    PoleJointUseTypes=PoleJointUseType.objects.all().order_by('Name')
    PoleJointUseTypeChoices.append(['-1', 'None'])
    for i in PoleJointUseTypes:
        PoleJointUseTypeChoices.append([i.id, str(i)])
    #Create PoleJointUseCompanyChoices
    PoleJointUseCompanyChoices=[]
    PoleJointUseCompanies=Company.objects.all().filter(PoleJointUseIndicator=True).order_by('Name')
    PoleJointUseCompanyChoices.append(['-1', 'None'])
    for i in PoleJointUseCompanies:
        PoleJointUseCompanyChoices.append([i.id, str(i)])
    for i in range(0, 6):
        Dict['PoleJointUseType' + str(i)]=forms.ChoiceField(PoleJointUseTypeChoices, required=False)
        Dict['PoleJointUseCompany' + str(i)]=forms.ChoiceField(choices=PoleJointUseCompanyChoices, required=False)
    return Dict

def ProcessShortcuts(form, Shortcuts, SelectionStation, UserName):
    for i in Shortcuts:
        try:
            CreateIndicator=bool(form.data['StationAssemblyShortcut' + i + 'CreateIndicator'] == 'on')
        except:
            CreateIndicator=False
        try:
            PrimaryIndicator=bool(form.data['StationAssemblyShortcut' + i + 'PrimaryIndicator'] == 'on')
        except:
            PrimaryIndicator=False
        try:
            SecondaryIndicator=bool(form.data['StationAssemblyShortcut' + i + 'SecondaryIndicator'] == 'on')
        except:
            SecondaryIndicator=False
        try:
            ServiceIndicator=bool(form.data['StationAssemblyShortcut' + i + 'ServiceIndicator'] == 'on')
        except:
            ServiceIndicator=False
        try:
            Copies=int(form.data['StationAssemblyShortcut' + i + 'NumberOfCopies'])
        except:
            Copies=1
        try:
            CircuitNumber=int(form.data['StationAssemblyShortcut' + i + 'CircuitNumber'])
        except:
            CircuitNumber = None
        if(Copies > 9):
            Copies = 9
        if(CreateIndicator and CircuitNumber):
            for copy in range(0, Copies):
                QuickCreateAssemblies(Station=SelectionStation, AssemblyNameList=Shortcuts[i], UserName=UserName,
                                 PrimaryIndicator=PrimaryIndicator, SecondaryIndicator=SecondaryIndicator,
                                 ServiceIndicator=ServiceIndicator, CircuitNumber=CircuitNumber)

def QuickCreateAssemblies(Station, AssemblyNameList, UserName, PrimaryIndicator, SecondaryIndicator, ServiceIndicator, CircuitNumber):
    for assemblyName in AssemblyNameList:
        command=Command()
        command=command.Create(Name='CreateStationAssembly', AssemblyName=assemblyName, StartPoint=Station.Point, 
                               PrimaryIndicator=PrimaryIndicator, SecondaryIndicator=SecondaryIndicator, 
                               CircuitNumber=CircuitNumber, ServiceIndicator=ServiceIndicator, CommandUserName=UserName, 
                               InstallStatus='Existing', PhaseAIndicator=False, PhaseBIndicator=False, PhaseCIndicator=False,
                               PhaseNeutralIndicator=False, IdleIndicator=False, TagNumber='', OpenIndicator=False, SerialNumber='',
                               MapNumber='', OldMapNumber='', MeterDCSINumber='', MeterNumber='', MeterOnPoleIndicator=False,
                               StakerWorkOrderid=0, StakerWorkOrderIndexWhenReverseSorted=-1)
        assembly=command.Process()
       
def ProcessDeleteAssembly(form, SelectionStation, SelectionSpan, UserName):
    #Check to see if a Station or Span Assembly Should be deleted
    DeleteCompatibleUnits=[]
    StationIndicator=False
    SpanIndicator=False
    try:
        DeleteAssembly=form.data['DeleteAssembly']
    except:
        return
    DeleteKeep=COOPGIS_FormMisc.FormGetInteger(form, 'DeleteKeep', 0)
    AssembliesToDelete=[]
    if(DeleteAssembly == '*STATION*'):
        StationIndicator=True
        Assemblies=Assembly.objects.filter(Station=SelectionStation).exclude(CompatibleUnit__PoleIndicator=True)
        for i in Assemblies:
            AssembliesToDelete.append(i)
    elif(DeleteAssembly == '*SPAN*'):
        SpanIndicator=True
        Assemblies=Assembly.objects.filter(Span=SelectionSpan)
        for i in Assemblies:
            AssembliesToDelete.append(i)
    else:
        if(SelectionStation):
            Assemblies=Assembly.objects.filter(Station=SelectionStation).filter(CompatibleUnit__Name=DeleteAssembly)
            for i in Assemblies:
                AssembliesToDelete.append(i)
                StationIndicator=True
        if(SelectionSpan):
            Assemblies=Assembly.objects.filter(Span=SelectionSpan).filter(CompatibleUnit__Name=DeleteAssembly)
            for i in Assemblies:
                AssembliesToDelete.append(i)
                SpanIndicator=True
    if(len(AssembliesToDelete) > DeleteKeep):
        AssembliesToDelete=AssembliesToDelete[DeleteKeep:]
    else:
        return
    if(StationIndicator):
        for assembly in AssembliesToDelete:
            command=Command()
            command=command.Create(Name='DeleteStationAssembly', StartPoint=SelectionStation.Point, AssemblyName=assembly.CompatibleUnit.Name, 
                                   CommandUserName=UserName)
            command.Process()
    elif(SpanIndicator):
        for assembly in AssembliesToDelete:
            command=Command()
            command=command.Create(Name='DeleteSpanAssembly', StartPoint=SelectionSpan.StartStation.Point, EndPoint=SelectionSpan.EndStation.Point,
                                   Line=SelectionSpan.Line, AssemblyName=assembly.CompatibleUnit.Name, 
                                   CommandUserName=UserName)
            command.Process()

def ProcessPoleRightOfWayStatus(form, SelectionStation, UserName):
    try:
        PoleRightOfWayStatusid=int(form.data['PoleRightOfWayStatus'])
    except:
        PoleRightOfWayStatusid=None
    if(SelectionStation and (not PoleRightOfWayStatusid == -1) and PoleRightOfWayStatusid):
        poleRightOfWayStatus=PoleRightOfWayStatus.objects.filter(id=PoleRightOfWayStatusid)[0]
        command=Command()
        command=command.Create(Name='UpdatePoleRightOfWayStatus', StartPoint=SelectionStation.Point, PoleRightOfWayStatusName=poleRightOfWayStatus.Name, 
                       CommandUserName=UserName)
        UpdatedPoleRightOfWayStatusIndicator=command.Process()

def ProcessPoleJointUse(form, SelectionStation, UserName):
    try:
        PoleJointUseTypeid=int(form.data['StationAssemblyPoleJointUseType1'])
    except:
        PoleJointUseTypeid=None
    try:
        PoleJointUseCompanyid=int(form.data['StationAssemblyPoleJointUseCompany1'])
    except:
        PoleJointUseCompanyid=None
    if(SelectionStation and (not PoleJointUseTypeid == -1) and PoleJointUseTypeid and PoleJointUseCompanyid and (not PoleJointUseCompanyid == -1)):
       poleJointUseType=PoleJointUseType.objects.filter(id=PoleJointUseTypeid)[0]
       poleJointUseCompany=Company.objects.filter(id=PoleJointUseCompanyid)[0]
       poleJointUse=PoleJointUse()
       command=Command()
       command=command.Create(Name='CreatePoleJointUse', StartPoint=SelectionStation.Point, PoleJointUseTypeName=poleJointUseType.Name, 
                                         CompanyName=poleJointUseCompany.Name, CommandUserName=UserName)
       poleJointUse1Update=command.Process()

def ProcessPoleTreatment(form, SelectionStation, UserName):
    try:
        PoleTreatmentTypeid=int(form.data['PoleTreatmentType'])
    except:
        PoleTreatmentTypeid=None
    try:
        PoleTreatmentCompanyid=int(form.data['PoleTreatmentCompany'])
    except:
        PoleTreatmentCompanyid=None
    try:
        Year=int(form.data['PoleTreatmentDateYear'])
        PoleTreatmentDate=datetime.datetime(Year, 1, 1)
    except:
        PoleTreatmentDate=datetime.datetime(1925, 1, 1)
    if(SelectionStation and (not PoleTreatmentTypeid == -1) and PoleTreatmentTypeid and PoleTreatmentCompanyid and (not PoleTreatmentCompanyid == -1)):
       try:
           pole=Assembly.objects.filter(Station=SelectionStation, CompatibleUnit__PoleIndicator=True)[0]
       except:
           return
       poleTreatmentType=PoleTreatmentType.objects.filter(id=PoleTreatmentTypeid)[0]
       poleTreatmentCompany=Company.objects.filter(id=PoleTreatmentCompanyid)[0]
       if(pole):
           command=Command()
           command=command.Create(Name='CreatePoleTreatment', StartPoint=SelectionStation.Point, PoleTreatmentTypeName=poleTreatmentType.Name, 
                                  CompanyName=poleTreatmentCompany.Name, PoleTreatmentDate=PoleTreatmentDate, CommandUserName=UserName)
           CreatedPoleTreatment=command.Process()

def PopulateInventoryCollectionFormFields(form, Shortcuts, SelectionStation, SelectionSpan):
    for i in range(0, 10): #Number of Station Assemblies Listed On the Form
        form.data['StationAssemblyCompatibleUnit' + str(i)]=-1
        form.data['StationAssemblyPhaseAIndicator' + str(i)]=False
        form.data['StationAssemblyPhaseBIndicator' + str(i)]=False
        form.data['StationAssemblyPhaseCIndicator' + str(i)]=False
        form.data['StationAssemblyPhaseNeutralIndicator' + str(i)]=False
        form.data['StationAssemblyPrimaryIndicator' + str(i)]=False
        form.data['StationAssemblySecondaryIndicator' + str(i)]=False
        form.data['StationAssemblyServiceIndicator' + str(i)]=False
        form.data['StationAssemblyOpenIndicator' + str(i)]=False
        form.data['StationAssemblyRotation' + str(i)]=0
        form.data['StationAssemblyMeterNumber' + str(i)]=''
        form.data['StationAssemblySerialNumber' + str(i)]=''
        form.data['StationAssemblyMeterOnPoleIndicator' + str(i)]=False
        form.data['StationAssemblyMeterDCSINumber' + str(i)]=''
        form.data['StationAssemblyMeterForm' + str(i)]=''
        form.data['StationAssemblyMapNumber' + str(i)]=''
        form.data['StationAssemblyOldMapNumber' + str(i)]=''
        form.data['StationAssemblyOwner' + str(i)]=-1
        form.data['StationAssemblyManufacturer' + str(i)]=-1    
        form.data['StationAssemblyTagNumber' + str(i)]=''
        form.data['StationAssemblyPutIntoServiceDateYear' + str(i)]=''    
        form.data['StationAssemblyPrimaryIndicator' + str(i)]=False
        form.data['StationAssemblySecondaryIndicator' + str(i)]=False
        form.data['StationAssemblyPoleStubIndicator' + str(i)]=False
        form.data['StationAssemblyPoleLightIndicator' + str(i)]=False
        form.data['StationAssemblyPoleMeterIndicator' + str(i)]=False
        form.data['StationAssemblyMeterServiceType' + str(i)]=-1
        form.data['StationAssemblyIdleIndicator' + str(i)]=False
        form.data['StationAssemblyPhysicalAddress' + str(i)]=''
        form.data['StationAssemblyNumberOfCopies' + str(i)]=1
        form.data['StationAssemblyTransformerCutOutIndicator' + str(i)]=False
    for i in range(0, 3):
        form.data['SpanAssemblyCompatibleUnit' + str(i)]=-1
        form.data['SpanAssemblyPhaseAIndicator' + str(i)]=False
        form.data['SpanAssemblyPhaseBIndicator' + str(i)]=False
        form.data['SpanAssemblyPhaseCIndicator' + str(i)]=False
        form.data['SpanAssemblyPhaseNeutralIndicator' + str(i)]=False
        form.data['SpanAssemblyPrimaryIndicator' + str(i)]=False
        form.data['SpanAssemblySecondaryIndicator' + str(i)]=False
        form.data['SpanAssemblyServiceIndicator' + str(i)]=False
        form.data['SpanAssemblyIdleIndicator' + str(i)]=False
    #General Fields for All Stations/Spans
    form.data['SelectGPSIndicator']=False
    form.data['CreateStationIndicator']=False
    form.data['MoveStationGPSIndicator']=False
    form.data['MoveStationNorth']=''
    form.data['MoveStationEast']=''
    if(SelectionStation):
        form.setData({'StationComment':SelectionStation.Description})
    else:
        form.data['StationComment']=''
    form.data['UpdateStationCommentIndicator']=False
    form.data['DeleteStationIndicator']=False
    form.data['CreateSpanIndicator']=False
    if(SelectionSpan):
        form.data['SpanComment']=SelectionSpan.Description
        form.data['SpanTapIndicator']=SelectionSpan.TapIndicator
        form.data['SpanJointTrenchIndicator']=SelectionSpan.JointTrenchIndicator
    else:
        form.data['SpanComment']=''
        form.data['SpanTapIndicator']=False
        form.data['SpanJointTrenchIndicator']=False
    form.data['UpdateSpanCommentIndicator']=False
    form.data['DeleteSpanIndicator']=False
    form.data['DeleteAssembly']=''
    form.setData({'PoleTreatmentType':-1})
    #Create a shortcut dictionary that has each shortcut along with the units that the shortcut will add
    for i in Shortcuts:
        form.data['StationAssemblyShortcut' + i + 'CreateIndicator']=False
        form.data['StationAssemblyShortcut' + i + 'NumberOfCopies']=1
        form.data['StationAssemblyShortcut' + i + 'PrimaryIndicator']=False
        form.data['StationAssemblyShortcut' + i + 'SecondaryIndicator']=False
        form.data['StationAssemblyShortcut' + i + 'ServiceIndicator']=False    
    for i in range(0, 6):
        form.data['PoleJointUseType' + str(i)]=-1
        form.data['PoleJointUseCompany' + str(i)]=-1
    pass






















